package com.notes.romania.services;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;

import com.notes.romania.broadcastreceiver.AlarmBroadcastReceiver;
import com.notes.romania.reminder.RemManager;

public class BackgroundService extends Service{
		private AlarmManager am;
		
		private SharedPreferences settings;
    	private SharedPreferences.Editor editor;
    	
	 	@Override
	 	public IBinder onBind(Intent intent) {
	      return null;
	 	}
	
	 	@Override
	 	public void onCreate() {
	 		settings = getSharedPreferences(RemManager.PREFS_NAME, Context.MODE_PRIVATE);
	 		editor = settings.edit();
	 		
	 		RemManager.setUp(settings, editor);
	 		
	 		//Start looking
	 		am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
	 		setRepeatingAlarm(30000);
	 	}
	
	 	@Override
	 	public void onDestroy() {
	 		//code to execute when the service is shutting down
	 	}
	
	 	@Override
	 	 public int onStartCommand(Intent intent, int flags, int startId) {
			return START_STICKY;
	 	
	 	}
	 	
	 	private void setRepeatingAlarm(int millis){
	 		 Intent intent = new Intent(getApplicationContext(), AlarmBroadcastReceiver.class);
	 		 PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0,
	 				 intent, 0);
	 		 am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 2000, millis, pendingIntent);
	 		
	 	}
}
